/**
 * @fileoverview To work around the same origin policy, GData requests must be
 * proxied. This class should be used instead of XMLHttpRequest to proxy such
 * requests.
 * @author bolinfest@gmail.com (Michael Bolin)
 */
goog.provide('gdata.Xhr');

/**
 * @constructor
 */
gdata.Xhr = function() {
  /**
   * @type {!XMLHttpRequest}
   * @private
   */
  this.xhr_ = new XMLHttpRequest();
};

/**
 * @param {Function} onreadystatechange
 */
gdata.Xhr.prototype.setOnReadyStateChange = function(onreadystatechange) {
  this.xhr_.onreadystatechange = onreadystatechange;
};

/**
 * @param {string} method
 * @param {string} url
 */
gdata.Xhr.prototype.open = function(method, url) {
  var asynchronous = true;
  url = '/feedproxy?feedUrl=' + encodeURIComponent(url);
  this.xhr_.open(method, url, asynchronous);
  gdata.Xhr.addXsrfProtection(this.xhr_);
};

/**
 * @param {XMLHttpRequest} xhr its open() method must have already been called
 */
gdata.Xhr.addXsrfProtection = function(xhr) {
  xhr.setRequestHeader('no-xsrf', '1');
};

/**
 * @param {string} name
 * @param {string} value
 */
gdata.Xhr.prototype.setRequestHeader = function(name, value) {
  this.xhr_.setRequestHeader(name, value);  
};

/**
 * @param {?Object} data
 */
gdata.Xhr.prototype.send = function(data) {
  this.xhr_.send(data);  
};

/**
 * @return {?number}
 */
gdata.Xhr.prototype.getReadyState = function() {
  return this.xhr_.readyState;
};

/**
 * @return {?number}
 */
gdata.Xhr.prototype.getStatus = function() {
  return this.xhr_.status;  
};

/**
 * @return {?string}
 */
gdata.Xhr.prototype.getResponseText = function() {
  return this.xhr_.responseText;  
};
